layui.use(['form','layer','laydate'],function(){
    var form = layui.form,
    	laydate = layui.laydate,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    
    laydate.render({ elem: '#date' });
    
    var inputObj = $("#recordDiv").find("input");
    inputObj.bind("keyup", function (event){
    	 if (event.keyCode == "13") {
 	      	loadDorm();
         }
    });
    function loadDorm() {
    	var searchKey = inputObj.val();
      	$.ajax({
      		type: 'get',
      		url: '../dormbuild/listForSelect.html',
      		data:{searchKey: searchKey},
      		timeout:1500,
      		success:function(msg) {
      			var select = $('#recordSelect');
      			var options = "";//select.html();
      			var data = msg.data;
      			for (var i in data) {
      				options += "<option value='"+data[i].dormBuildId+"'>"+data[i].dormBuildName+"</option>";
      			}
      			select.html(options);
      			form.render('select');
      			//渲染后要重新绑定
      			inputObj = $("#recordDiv").find("input");
      			inputObj.click();
      			inputObj.bind("keyup", function (event){
      				if (event.keyCode == "13") {
      		        	loadDorm();
      		        }
      		    });
      			//课程的搜索绑定
      			inputObj2 = $("#baseStuDiv").find("input");
      			inputObj2.bind("keyup", function (event){
      				if (event.keyCode == "13") {
      		        	loadStu();
      		        }
      		    });
      		}
      	});
    }
    
    var inputObj2 = $("#baseStuDiv").find("input");
    inputObj2.bind("keyup", function (event){
    	if (event.keyCode == "13") {
        	loadStu();
        }
    });
    function loadStu() {
    	var searchKey = inputObj2.val();
      	$.ajax({
      		type: 'get',
      		url: '/student/listForSelect.html',
      		data:{searchKey: searchKey},
      		timeout:1500,
      		success:function(msg) {
      			var select = $('#baseStuSelect');
      			var options = "";
      			var data = msg.data;
      			for (var i in data) {
      				options += "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
      			}
      			select.html(options);
      			form.render('select');
      			//渲染后要重新绑定
      			inputObj = $("#recordDiv").find("input");
      			inputObj.bind("keyup", function (event){
      				if (event.keyCode == "13") {
      		        	loadDorm();
      		        }
      		    });
      			//课程的搜索绑定
      			inputObj2 = $("#baseStuDiv").find("input");
      			inputObj2.click();
      			inputObj2.bind("keyup", function (event){
      				if (event.keyCode == "13") {
      		        	loadStu();
      		        }
      		    });
      		}
      	});
    }
    
    $(document).ready(function (){
    	if (inputObj.val() == null || inputObj.val() == "") {
        	loadDorm();
        	loadStu();
        }
    });
    
    
    form.on("submit(addRecord)",function(data){
    	//弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
    	var recordId = $("#recordId").val();
    	//console.log("oldId:"+recordId)
    	if (recordId == null || recordId == "") {
    		recordId = 0;
		}
    	//console.log("newId:"+recordId)
        $.ajax({
    		type: 'post',
    		url: '../record/add.html',
    		data: {
    			opType : $("#opType").val(),
    			recordId : recordId,
				dormBuildId : $("#recordSelect").val(),
	            stuId: $("#baseStuSelect").val(),
	            date: $("#date").val(),
	            detail: $("#detail").val(),
	            dormName : $("#dormName").val()
			},
			timeout:2000,
			success : function(msg) {
				top.layer.close(index);
				if (msg == true) {
					if ($("#opType").val() == 1) {// 是修改操作
						// 修改成功则刷新父页面
						layer.closeAll("iframe");
						parent.location.reload();
					} else {
						top.layer.msg("操作成功！");
						parent.location.reload();
					}
				} else {
					layer.msg(msg, {
						icon : 5,
						time : 1000
					});
				}
			},
			error : function() {
				layer.msg("操作失败！", {
					icon : 5,
					time : 1000
				});
				layer.close(index);
			}
		});
		return false;
    });
});